

//
//  ZYUnratifiedController.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/25.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYUnratifiedController.h"
#import "ZYUnratifiedsCell.h"
#import "ZYUnratifiedsModel.h"
#import "HMGradingAssignmentVC.h"
#define kCell @"cell"
@interface ZYUnratifiedController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,copy) NSString *searchStr;
@end

@implementation ZYUnratifiedController


- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorRGB(247,245, 245);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nsnotifiAction:) name:@"zyOne" object:nil];

    [self setUpSuperView];
    [self setUpRefresh];
}


- (void)nsnotifiAction:(NSNotification *)nsnotification{
    
    self.searchStr = [nsnotification valueForKey:@"object"];
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}










- (void)setNetWork{
    
    //http://192.168.0.113:8080/StarShine/ys/assignHomework.do?action=getPostilList&workId=10&type=3
    NSString *url = [NSString stringWithFormat:@"assignHomework.do?action=getPostilList&searchName=%@&workId=%@&type=1&pageNumber=%ld",self.searchStr?self.searchStr:@"",self.ID,(long)self.page];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    NSString *changeUilStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            NSArray *dataList = [t valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in dataList) {
                
                ZYUnratifiedsModel *model = [ZYUnratifiedsModel new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                
                
                
                
                [self.dataSource addObject:model];
                
                
            }
            
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
        
        
        
    } Fail:^(NSError *Error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
}

























- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(8,0, kScreenWidth - 16, kScreenHeight - 64 -44)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    [self.tableView registerClass:[ZYUnratifiedsCell class] forCellReuseIdentifier:kCell];
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight *20/1334)];
    self.allView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.tableHeaderView = self.allView;
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYUnratifiedsCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    if (self.dataSource.count > indexPath.row) {
        
        ZYUnratifiedsModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYUnratifiedsModel:model];
        
        
    }
    [cell.markingButton addTarget:self action:@selector(markingButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"zyOne" object:nil];
    
    
    
}



- (void)markingButtonAction:(UIButton *)sender{
    
    
    ZYUnratifiedsCell *cell = (ZYUnratifiedsCell *)sender.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYUnratifiedsModel *model = self.dataSource[indexPath.row];
    HMGradingAssignmentVC *vc = [HMGradingAssignmentVC new];
    __weak typeof(self) weakSelf = self;
    vc.successBlock = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    vc.answerId = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
    
}










- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}



@end

